﻿using System;
//using System::IO;
//using System::Net;
//using System::Text;

using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;


namespace ImMT
{
    class Program
    {
        static public StreamWriter writer;
        static public StreamWriter gf;
        static public StreamWriter drop;
        static public StreamWriter ativeSkill;
        static public StreamWriter nSkill;
        static public StreamWriter pSkill;
        static public StreamWriter Skillup;

        static public StreamWriter fuben;
        static public StreamWriter fubendrop;
        static public StreamWriter raid;
        static public int fubenId;

        static public bool[] useMask;
        static public Dictionary<string, int> imgDict;
        static public int serialId;

        //主程式
        static void Main(string[] args)
        {
            //cardDownload();
            fubenDownload();

        }

        static public void fubenDownload()
        {
            fubenId = 41;
            fuben = new StreamWriter("fuben.csv");
            fubendrop = new StreamWriter("fubendrop.csv");
            raid = new StreamWriter("raid.csv");
            WebClient csWC = new WebClient();
            byte[] html = csWC.DownloadData("http://mt.youba.com/fuben");
            String strhtml = Encoding.UTF8.GetString(html);

            int idx1, idx2;

            idx1 = strhtml.IndexOf("=\"", strhtml.IndexOf("<li class=\"fn-clear\">")+20);
            while (idx1 > 0)
            {
                idx1 += 2;
                idx2 = strhtml.IndexOf("1.htm", idx1);
                string url = strhtml.Substring(idx1, idx2 - idx1);

                idx1 = strhtml.IndexOf("》", idx1) + 1;
                idx2 = strhtml.IndexOf("<", idx1);

                string name = strhtml.Substring(idx1, idx2 - idx1);

                //singlefubenDownload1(url + "1.htm", name);
                singlefubenDownload2(url + "2.htm", name);

                idx1 = strhtml.IndexOf("=\"", strhtml.IndexOf("<li class=\"fn-clear\">", idx2) + 20);

                if (fubenId == 0) break;

                fubenId--;
            }
            

            fuben.Flush();
            fuben.Close();
            fubendrop.Flush();
            fubendrop.Close();
            raid.Flush();
            raid.Close();
        }

        static public void singlefubenDownload1(string url, string name)
        {
            if (name == "小金龙限时副本") return;
            if (name == "南瓜农场限时副本") return;
            if (name == "菊爆大队的试炼副本") return;
            if (name == "迪奥布斯的试炼副本") return;


            WebClient web = new WebClient();
            byte[] html = web.DownloadData(url);
            string str = Encoding.UTF8.GetString(html);

            int mode = 0;
            int idx1, idx2;
            string tag3;
            int stage = 0;

            idx1 = str.IndexOf("<TD vAlign=\"center\" width=", str.IndexOf("<TBODY>", 0, StringComparison.OrdinalIgnoreCase), StringComparison.OrdinalIgnoreCase) + 1;
            if (idx1 > 0)
            {
                idx2 = str.IndexOf("\">", idx1, StringComparison.OrdinalIgnoreCase);

                idx1 = str.IndexOf("<TD vAlign=\"center\" width=", idx2, StringComparison.OrdinalIgnoreCase) + 1;
                idx2 = str.IndexOf("\">", idx1, StringComparison.OrdinalIgnoreCase);

                idx1 = str.IndexOf("<TD vAlign=\"center\" width=", idx2, StringComparison.OrdinalIgnoreCase) + 1;
                idx2 = str.IndexOf("\">", idx1, StringComparison.OrdinalIgnoreCase);

                idx1 = str.IndexOf("<TD vAlign=\"center\" width=", idx2, StringComparison.OrdinalIgnoreCase) + 1;
                idx2 = str.IndexOf("\">", idx1, StringComparison.OrdinalIgnoreCase);
                tag3 = str.Substring(idx1, idx2 - idx1);
                mode = 0;
            }
            else
            {
                idx1 = str.IndexOf("<td width=", str.IndexOf("<TBODY>", 0, StringComparison.OrdinalIgnoreCase), StringComparison.OrdinalIgnoreCase) + 1;
                idx2 = str.IndexOf("\">", idx1, StringComparison.OrdinalIgnoreCase);

                idx1 = str.IndexOf("<td width=", idx2, StringComparison.OrdinalIgnoreCase) + 1;
                idx2 = str.IndexOf("\">", idx1, StringComparison.OrdinalIgnoreCase);

                idx1 = str.IndexOf("<td width=", idx2, StringComparison.OrdinalIgnoreCase) + 1;
                idx2 = str.IndexOf("\">", idx1, StringComparison.OrdinalIgnoreCase);

                idx1 = str.IndexOf("<td width=", idx2, StringComparison.OrdinalIgnoreCase) + 1;
                idx2 = str.IndexOf("\">", idx1, StringComparison.OrdinalIgnoreCase);
                tag3 = str.Substring(idx1, idx2 - idx1);                
                mode = 1;
            }

            int start, end;

            end = idx2;

            int span1=0, span2=0;
            bool haveitem = true;
            
            List<string> item = new List<string>();
            
            do
            {
                start = str.IndexOf("rowSpan", end, StringComparison.OrdinalIgnoreCase);
                
                idx1 = str.IndexOf("\"", start) + 1;
                idx2 = str.IndexOf("\"", idx1);
                span1 += int.Parse(str.Substring(idx1, idx2 - idx1));
                
                end = str.IndexOf("rowSpan", start + 1, StringComparison.OrdinalIgnoreCase);
                end = str.IndexOf("rowSpan", end + 1, StringComparison.OrdinalIgnoreCase);
                end = str.IndexOf("rowSpan", end + 1, StringComparison.OrdinalIgnoreCase);
                if (haveitem)
                {
                    idx1 = str.IndexOf("\"", end) + 1;
                    idx2 = str.IndexOf("\"", idx1);
                    span2 = int.Parse(str.Substring(idx1, idx2 - idx1));
                }
                if (haveitem)
                {
                    end = str.IndexOf("rowSpan", end + 1, StringComparison.OrdinalIgnoreCase);
                }

                if (end < 0) end = str.Length;
                string temp = str.Substring(start, end - start);
                string bossname;
                string money;
                string exp;

                if (mode == 0)
                {
                    idx1 = temp.IndexOf("P>", temp.IndexOf("rowSpan", 0, StringComparison.OrdinalIgnoreCase), StringComparison.OrdinalIgnoreCase) + 2;
                    idx2 = temp.IndexOf("<", idx1);
                    bossname = temp.Substring(idx1, idx2 - idx1);

                    idx1 = temp.IndexOf("P>", temp.IndexOf("rowSpan", idx2, StringComparison.OrdinalIgnoreCase), StringComparison.OrdinalIgnoreCase) + 2;
                    idx2 = temp.IndexOf("<", idx1);
                    money = temp.Substring(idx1, idx2 - idx1);

                    idx1 = temp.IndexOf("P>", temp.IndexOf("rowSpan", idx2, StringComparison.OrdinalIgnoreCase), StringComparison.OrdinalIgnoreCase) + 2;
                    idx2 = temp.IndexOf("<", idx1);
                    exp = temp.Substring(idx1, idx2 - idx1);

                    if (haveitem)
                    {
                        string tttt = "";

                        idx1 = temp.IndexOf("<P>", temp.IndexOf("rowSpan", idx2, StringComparison.OrdinalIgnoreCase), StringComparison.OrdinalIgnoreCase);
                        idx2 = temp.IndexOf(tag3, idx1);
                        tttt = temp.Substring(idx1, idx2 - idx1);

                        idx1 = tttt.IndexOf("<P>", 0, StringComparison.OrdinalIgnoreCase);
                        while (idx1 >= 0)
                        {
                            idx1 += 3;
                            idx2 = tttt.IndexOf("<", idx1);
                            //item += tttt.Substring(idx1, idx2 - idx1) + " ";
                            string s1 = tttt.Substring(idx1, idx2 - idx1);
                            string[] t1 = s1.Split(new Char[] { '\n', ' ', '\t', '、' });
                            foreach (string s in t1)
                            {
                                if (s.Length > 1) item.Add(s);
                            }
                            //item.Add(tttt.Substring(idx1, idx2 - idx1));
                            idx1 = tttt.IndexOf("<P>", idx2, StringComparison.OrdinalIgnoreCase);
                        }
                    }
                }
                else
                {
                    idx1 = temp.IndexOf("r\">", temp.IndexOf("rowSpan", 0, StringComparison.OrdinalIgnoreCase), StringComparison.OrdinalIgnoreCase) + 3;
                    idx2 = temp.IndexOf("<", idx1);
                    bossname = temp.Substring(idx1, idx2 - idx1);


                    idx1 = temp.IndexOf("r\">", temp.IndexOf("rowSpan", idx2, StringComparison.OrdinalIgnoreCase), StringComparison.OrdinalIgnoreCase) + 3;
                    idx2 = temp.IndexOf("<", idx1);
                    money = temp.Substring(idx1, idx2 - idx1);

                    idx1 = temp.IndexOf("r\">", temp.IndexOf("rowSpan", idx2, StringComparison.OrdinalIgnoreCase), StringComparison.OrdinalIgnoreCase) + 3;
                    idx2 = temp.IndexOf("<", idx1);
                    exp = temp.Substring(idx1, idx2 - idx1);

                    if (haveitem)
                    {
                        string tttt = "";

                        idx1 = temp.IndexOf("align", temp.IndexOf("rowSpan", idx2, StringComparison.OrdinalIgnoreCase), StringComparison.OrdinalIgnoreCase);
                        idx2 = temp.IndexOf(tag3, idx1);
                        tttt = temp.Substring(idx1, idx2 - idx1);

                        idx1 = tttt.IndexOf("\">", 0, StringComparison.OrdinalIgnoreCase);
                        while (idx1 >= 0)
                        {
                            idx1 += 2;
                            idx2 = tttt.IndexOf("<", idx1);
                            //item += tttt.Substring(idx1, idx2 - idx1) + " ";
                            //item.Add(tttt.Substring(idx1, idx2 - idx1));
                            string s1 = tttt.Substring(idx1, idx2 - idx1);
                            string[] t1 = s1.Split(new Char[] { '\n', ' ', '\t', '、' });
                            foreach (string s in t1)
                            {
                                if (s.Length > 1) item.Add(s);
                            }
                            idx1 = tttt.IndexOf("\">", idx2, StringComparison.OrdinalIgnoreCase);
                        }
                    }
                }

                Console.WriteLine(name + bossname);


                List<string> card = new List<string>();
                //List<string> star = new List<string>();
                idx1 = temp.IndexOf(tag3);
                while (idx1 > 0)
                {
                    if (mode == 0)
                    {
                        idx1 = temp.IndexOf("P>", idx1, StringComparison.OrdinalIgnoreCase) + 2;
                        idx2 = temp.IndexOf("<", idx1);
                        //card += temp.Substring(idx1, idx2 - idx1);
                        //card.Add(temp.Substring(idx1, idx2 - idx1));
                        string s1 = temp.Substring(idx1, idx2 - idx1);

                        idx1 = temp.IndexOf("<P>", idx2, StringComparison.OrdinalIgnoreCase) + 3;
                        idx2 = temp.IndexOf("<", idx1);
                        //card += "," + temp.Substring(idx1, idx2 - idx1);
                        card.Add(s1 + "," + temp.Substring(idx1, idx2 - idx1));

                    }
                    else
                    {
                        idx1 = temp.IndexOf("align", idx1, StringComparison.OrdinalIgnoreCase);
                        if (idx1 < 0) break;
                        idx1 = temp.IndexOf("\">", idx1, StringComparison.OrdinalIgnoreCase) + 2;
                        idx2 = temp.IndexOf("<", idx1);
                        //card += temp.Substring(idx1, idx2 - idx1);
                        string s1 = temp.Substring(idx1, idx2 - idx1);
                        //card.Add(temp.Substring(idx1, idx2 - idx1));

                        idx1 = temp.IndexOf("r\">", idx2, StringComparison.OrdinalIgnoreCase) + 3;
                        idx2 = temp.IndexOf("<", idx1);
                        //card += "," + temp.Substring(idx1, idx2 - idx1);
                        card.Add(s1 + "," + temp.Substring(idx1, idx2 - idx1));
                    }

                    idx1 = temp.IndexOf(tag3, idx2);
                   
                }
                fuben.WriteLine(fubenId + "," + stage + "," + name + "," + bossname + "," + money + "," + exp);

                foreach (string s in item)
                {
                    fubendrop.WriteLine(fubenId + "," + stage + "," + s + ",0");
                }
                foreach (string s in card)
                {
                    fubendrop.WriteLine(fubenId + "," + stage + "," + s + ",1");
                }
                fuben.Flush();
                fubendrop.Flush();


                if (span1 == span2)
                {
                    span1 = 0;
                    haveitem = true;
                    item.Clear();
                }
                else
                {
                    haveitem = false;
                }

                stage++;

            } while (end < str.Length);

        }

        static public void singlefubenDownload2(string url, string name)
        {
            if (name == "小金龙限时副本") return;
            if (name == "南瓜农场限时副本") return;
            if (name == "菊爆大队的试炼副本") return;
            if (name == "迪奥布斯的试炼副本") return;


            WebClient web = new WebClient();
            byte[] html = web.DownloadData(url);
            string str = Encoding.UTF8.GetString(html);

            int start;
            int end;
            int stage = 0;

            
            end = 0;
            do
            {
                start = str.IndexOf("colSpan", end, StringComparison.OrdinalIgnoreCase);
                end = str.IndexOf("colSpan", start+1, StringComparison.OrdinalIgnoreCase);
                if (end < 0)
                    end = str.Length;

                string temp = str.Substring(start, end - start);

                int idx1 = temp.IndexOf("<P", 0, StringComparison.OrdinalIgnoreCase) + 2;
                int idx2 = temp.IndexOf("</P", idx1, StringComparison.OrdinalIgnoreCase);
                string tmp1 = temp.Substring(idx1, idx2 - idx1);

                idx1 = tmp1.IndexOf("<B>", 0, StringComparison.OrdinalIgnoreCase);
                if (idx1 < 0)
                {
                    idx1 = tmp1.IndexOf(">") + 1;
                    idx2 = tmp1.IndexOf("<", idx1);
                    if (idx2 < 0) idx2 = tmp1.Length;
                }
                else
                {
                    idx1 += 3;
                    idx2 = tmp1.IndexOf("<", idx1);
                }

                string bossname = tmp1.Substring(idx1, idx2 - idx1);

                if (bossname == "锁惢深大帝")
                {
                    int a = 0;
                }

                Console.WriteLine(name + bossname);

                string[] tag = { "第一回合", "第二回合", "第三回合" };

                for (int round = 0; round < 3; round++)
                {
                    idx1 = temp.IndexOf(tag[round], idx2, StringComparison.OrdinalIgnoreCase);

                    List<string> seat = new List<string>();
                    for (int i = 0; i < 2; i++)
                    {
                        idx1 = temp.IndexOf("<TR>", idx1, StringComparison.OrdinalIgnoreCase);
                        idx2 = temp.IndexOf("</TR>", idx1, StringComparison.OrdinalIgnoreCase);
                        string array = temp.Substring(idx1, idx2 - idx1);

                        int pos1 = 0;
                        int pos2 = 0;
                        for (int j = 0; j < 3; j++)
                        {
                            pos1 = array.IndexOf("<TD", pos1, StringComparison.OrdinalIgnoreCase);
                            pos2 = array.IndexOf("</TD>", pos1, StringComparison.OrdinalIgnoreCase);

                            string npc = array.Substring(pos1, pos2 - pos1);

                            int a1 = npc.IndexOf("<P", 0, StringComparison.OrdinalIgnoreCase);
                            int a2;
                            if (a1 < 0)
                            {
                                seat.Add("");
                            }
                            else
                            {
                                a1 = npc.IndexOf(">", npc.IndexOf("<P", 0, StringComparison.OrdinalIgnoreCase)) + 1;
                                a2 = npc.IndexOf("<", a1);
                                seat.Add(npc.Substring(a1, a2 - a1));
                            }

                            pos1 = array.IndexOf("<TD", pos2, StringComparison.OrdinalIgnoreCase);
                        }
                        idx1 = idx2;
                    }

                    //raid.Write(fubenId + "," + stage + "," + round + "," + i + ",");
                    int slot = 0;
                    foreach (string s in seat)
                    {
                        if (s.Length > 1)
                        {

                            string[] t1 = s.Split(new Char[] { '-', '+' });
                            string res = "";
                            foreach (string t in t1)
                            {
                                if (t.Length > 1)
                                    res += t + ",";
                            }
                            raid.WriteLine(fubenId + "," + stage + "," + round + "," + slot + "," + res);
                        }
                        else
                        {
                            //raid.Write("," + ",");
                            raid.WriteLine(fubenId + "," + stage + "," + round + "," + slot + "," + "," + ",");
                        }
                        slot++;
                    }
                    //raid.WriteLine();
                }

                if (end == str.Length) break;

                stage++;
            } while (true);

            raid.Flush();
        }


        static public void cardDownload()
        {
            writer = new StreamWriter("mt.csv");
            gf = new StreamWriter("gainfrom.csv");
            drop = new StreamWriter("drop.csv");
            ativeSkill = new StreamWriter("ativeSkill.csv");
            nSkill = new StreamWriter("normalSkill.csv");
            pSkill = new StreamWriter("passiveSkill.csv");
            Skillup = new StreamWriter("Skillup.csv");

            imgDict = new Dictionary<string, int>();
            serialId = 0;

            useMask = new bool[6000];
            for (int j = 0; j < 6000; j++) useMask[j] = false;

            for (int i = 0; i < 500; i++)
            {
                download(i, i, false);
            }

            writer.Flush();
            writer.Close();

            gf.Flush();
            gf.Close();

            drop.Flush();
            drop.Close();

            ativeSkill.Flush();
            ativeSkill.Close();

            nSkill.Flush();
            nSkill.Close();

            pSkill.Flush();
            pSkill.Close();

            Skillup.Flush();
            Skillup.Close();
        }

        static public void download(int group, int index, bool nest)
        {
            if (index % 1000 > 490) return;
            if (useMask[index]) return;
            Thread.Sleep(100);

            WebClient csWC = new WebClient();

            byte[] html = csWC.DownloadData("http://db.duowan.com/mt/card/detail/" + index + ".html");
            String strhtml = Encoding.UTF8.GetString(html);

            int idx1 = strhtml.IndexOf("card", strhtml.IndexOf("<h1"));
            int idx2 = strhtml.IndexOf("\"", idx1);
            string color = strhtml.Substring(idx1, idx2 - idx1);

            //星星

            int star = 1;
            if (color.Equals("cardColorwhite"))
                star = 2;
            else if (color.Equals("cardColorgreen"))
            {
                star = 3;
            }
            else if (color.Equals("cardColorblue"))
            {
                star = 4;
            }
            else if (color.Equals("cardColorpurple"))
            {
                star = 5;
            }
            else if (color.Equals("cardColororange"))
            {
                star = 6;
            }
            else
            {
                star = 1;
            }

            //名稱
            idx1 = strhtml.IndexOf(">", idx2) + 1;
            idx2 = strhtml.IndexOf("<", idx1);
            string title = strhtml.Substring(idx1, idx2 - idx1);

            if (title.Length == 0)
            {
                Console.WriteLine(index + " :not found!");
                return;
            }

            Console.WriteLine(index + ":" + title);

            //資訊
            string info = "";
            int infoidx = strhtml.IndexOf("<td colspan=\"5\" class=\"center\">");
            if (infoidx > 0)
            {
                idx1 = strhtml.IndexOf("\">", infoidx) + 2;
                idx2 = strhtml.IndexOf("<", idx1);
                if (idx2 > idx1)
                    info = strhtml.Substring(idx1, idx2 - idx1);
            }

            //職業
            idx1 = strhtml.IndexOf(">", strhtml.IndexOf("http://db.duowan.com/mt/card/list.html?conditions[profession]=")) + 1;
            idx2 = strhtml.IndexOf("<", idx1);
            string profession = strhtml.Substring(idx1, idx2 - idx1);


            //領導力
            idx1 = strhtml.IndexOf("<td>", strhtml.IndexOf("所需领导力")) + 4;
            idx2 = strhtml.IndexOf("<", idx1);
            string leader = strhtml.Substring(idx1, idx2 - idx1);

            //最大等級
            idx1 = strhtml.IndexOf("<td>", strhtml.IndexOf("等级上限")) + 4;
            idx2 = strhtml.IndexOf("<", idx1);
            string maxLv = strhtml.Substring(idx1, idx2 - idx1);


            string baseTitle = group + "," + index + "," + star + "," + title + ",";



            //idx1 = strhtml.IndexOf("src=", strhtml.IndexOf("line-height: 100px")) + 1;
            idx1 = strhtml.IndexOf("http://s1.dwstatic.com");
            idx2 = strhtml.IndexOf("\"", idx1);
            string imgurl = strhtml.Substring(idx1, idx2 - idx1);

            int ImageId;
            if (imgDict.ContainsKey(imgurl))
            {
                ImageId = imgDict[imgurl];
            }
            else
            {
                ImageId = serialId;
                imgDict[imgurl] = serialId;
                getImage(group + "_" + serialId.ToString("D4") + ".png", imgurl);
                serialId++;
            }


            //獲得來源
            idx1 = strhtml.IndexOf("查看所有获得来源为");
            while (idx1 > 0)
            {
                idx1 = strhtml.IndexOf("“", idx1) + 1;
                idx2 = strhtml.IndexOf("”", idx1);
                gf.Write(baseTitle + strhtml.Substring(idx1, idx2 - idx1));
                gf.Write("\n");
                idx1 = strhtml.IndexOf("查看所有获得来源为", idx2);
            }

            //掉落
            idx1 = strhtml.IndexOf("<td>", strhtml.IndexOf("掉落</strong>")) + 4;
            idx2 = strhtml.IndexOf("<", idx1);
            string getter = strhtml.Substring(idx1, idx2 - idx1);
            string[] split = getter.Split(new Char[] { '、' });
            foreach (string s in split)
            {
                if (s.Trim() != "")
                    drop.Write(baseTitle + s + "\n");
            }

            //主動技能
            idx1 = strhtml.IndexOf("\">", strhtml.IndexOf("主动技能</strong>")) + 2;
            idx2 = strhtml.IndexOf("<", idx1);
            string initiativeSkill = strhtml.Substring(idx1, idx2 - idx1);
            idx1 = strhtml.IndexOf("\">", idx2) + 2;
            idx2 = strhtml.IndexOf("<", idx1);
            ativeSkill.Write(baseTitle + initiativeSkill + "," + strhtml.Substring(idx1, idx2 - idx1) + "\n");


            //普通技能
            idx1 = strhtml.IndexOf("<td>", strhtml.IndexOf("普通技能")) + 4;
            idx2 = strhtml.IndexOf("<", idx1);
            string normalSkill = strhtml.Substring(idx1, idx2 - idx1);
            idx1 = strhtml.IndexOf("\">", idx2) + 2;
            idx2 = strhtml.IndexOf("<", idx1);
            nSkill.Write(baseTitle + normalSkill + "," + strhtml.Substring(idx1, idx2 - idx1) + "\n");

            //被動技能
            idx1 = strhtml.IndexOf("\">", strhtml.IndexOf("被动技能</strong>")) + 2;
            idx2 = strhtml.IndexOf("<", idx1);
            string passiveSkill = strhtml.Substring(idx1, idx2 - idx1);
            idx1 = strhtml.IndexOf("\">", idx2) + 2;
            idx2 = strhtml.IndexOf("<", idx1) - 2;

            if (idx2 <= idx1)
                pSkill.Write(baseTitle + passiveSkill + "\n");//無
            else pSkill.Write(baseTitle + passiveSkill + "," + strhtml.Substring(idx1, idx2 - idx1) + "\n");


            //技能升級卡牌出處
            idx1 = strhtml.IndexOf("<a title=\"卡牌");
            while (idx1 > 0)
            {
                idx1 = strhtml.IndexOf("：", idx1) + 1;
                idx2 = strhtml.IndexOf("\"", idx1);

                string t1 = strhtml.Substring(idx1, idx2 - idx1);
                string[] s1 = t1.Split(new Char[] { ' ', '：', '/' });

                string percent = "";
                foreach (string s in s1)
                {
                    if (s.IndexOf("%") > 0)
                    {
                        percent = s;
                        break;
                    }
                }

                Skillup.Write(baseTitle + s1[0] + "," + percent + "," + t1 + "\n");
                idx1 = strhtml.IndexOf("<a title=\"卡牌", idx2);
            }

            writer.Write(group);
            writer.Write(",");
            writer.Write(index);
            writer.Write(",");
            writer.Write(ImageId);
            writer.Write(",");
            writer.Write(star);
            writer.Write(",");
            writer.Write(title);
            writer.Write(",");
            writer.Write(leader);
            writer.Write(",");
            writer.Write(maxLv);
            writer.Write(",");
            writer.Write(profession);
            writer.Write(",");

            writer.Write(getUserLevel(index, 1));
            writer.Write(",");
            writer.Write(getUserLevel(index, Int32.Parse(maxLv)));
            writer.Write(",");

            writer.Write(passiveSkill);
            writer.Write(",");
            writer.Write(normalSkill);
            writer.Write(",");
            writer.Write(initiativeSkill);
            writer.Write(",");
            writer.Write(info);

            writer.Write("\n");

            writer.Flush();
            gf.Flush();
            drop.Flush();
            ativeSkill.Flush();
            nSkill.Flush();
            pSkill.Flush();
            Skillup.Flush();
            useMask[index] = true;

            if (!nest)
            {
                idx1 = strhtml.IndexOf(">", strhtml.IndexOf("选择升级起点")) + 1;
                idx2 = strhtml.IndexOf("select", idx1);
                string temp = strhtml.Substring(idx1, idx2 - idx1);

                idx1 = temp.IndexOf("=\"");
                while (idx1 > 0)
                {
                    idx1 += 2;
                    idx2 = temp.IndexOf("\"", idx1);
                    string id = temp.Substring(idx1, idx2 - idx1);
                    download(group, int.Parse(id), true);
                    idx1 = temp.IndexOf("=\"", idx2);
                }
            }
        }

        static public void getImage(string fileName, string img)
        {
            WebClient web = new WebClient();
            web.DownloadFile(img, "image\\" + fileName);

            /*
            byte[] image = web.DownloadData(img);
            FileStream fileStream = new FileStream("image\\" + fileName, FileMode.Create);
            fileStream.Write(image, 0, image.GetLength(0));
            fileStream.Flush();
            fileStream.Close();
             */
        }

        static public string getUserLevel(int id, int lv)
        {
            string output = "";
            WebClient json = new WebClient();
            byte[] html = json.DownloadData("http://db.duowan.com/mt/level/cardJSON.html?id=" + id + "&level=" + lv);
            String str = Encoding.UTF8.GetString(html);

            int idx1 = str.IndexOf(":", str.IndexOf("userHp")) + 1;
            int idx2 = str.IndexOf(",", idx1);
            output += str.Substring(idx1, idx2 - idx1);
            output += ",";

            idx1 = str.IndexOf(":", str.IndexOf("userAttack")) + 1;
            idx2 = str.IndexOf(",", idx1);
            output += str.Substring(idx1, idx2 - idx1);
            output += ",";

            idx1 = str.IndexOf(":", str.IndexOf("userExp")) + 1;
            idx2 = str.IndexOf(",", idx1);
            output += str.Substring(idx1, idx2 - idx1);
            output += ",";

            idx1 = str.IndexOf(":", str.IndexOf("userSale")) + 1;
            idx2 = str.IndexOf(",", idx1);
            output += str.Substring(idx1, idx2 - idx1);
            output += ",";

            idx1 = str.IndexOf(":", str.IndexOf("levelUpNeed")) + 2;
            idx2 = str.IndexOf("\"", idx1);
            string exp = str.Substring(idx1, idx2 - idx1);
            if (exp.Equals("\\u5df2\\u7ecf\\u662f\\u6700\\u9ad8\\u7b49\\u7ea7"))
                exp = "-1";

            output += exp;

            return output;
        }
    }
}
